package com.taptech.doufu.ui.adapter;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.taptech.doufu.R;

import java.util.List;

/**
 * Created by Wyatt on 2017/9/10/010.
 */

public class SearchHistoryAdapter extends BaseAdapter{

    public interface OnListListen {
        void onListChange(List<String> list);
        void onClick(String key);
    }


    private List<String> list;
    private OnListListen onListListen;

    public SearchHistoryAdapter(List<String> list,OnListListen onListListen){
        this.list = list;
        this.onListListen = onListListen;
    }

    @Override
    public int getCount() {
        if (list != null) {
            return list.size();
        }
        return 0;
    }

    @Override
    public String getItem(int position) {
        if (list != null && list.size() > position) {
            return list.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final String key = getItem(position);
        if (TextUtils.isEmpty(key)) {
            return new View(parent.getContext());
        }
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_item_search_history, parent, false);
            viewHolder.ivDelete = (ImageView) convertView.findViewById(R.id.ivDelete);
            viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.tvTitle);
            viewHolder.lay = (LinearLayout) convertView.findViewById(R.id.lay);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
            if (viewHolder == null) {
                viewHolder = new ViewHolder();
                viewHolder.ivDelete = (ImageView) convertView.findViewById(R.id.ivDelete);
                viewHolder.lay = (LinearLayout) convertView.findViewById(R.id.lay);
                viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.tvTitle);
                convertView.setTag(viewHolder);
            }
        }
        viewHolder.tvTitle.setText(key);
        viewHolder.lay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onListListen != null) {
                    onListListen.onClick(key);
                }
            }
        });
        viewHolder.ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onListListen != null && list.size()>position) {
                    list.remove(position);
                    notifyDataSetChanged();
                    onListListen.onListChange(list);
                }
            }
        });
        return convertView;
    }


    public class ViewHolder{
        public TextView tvTitle;
        public ImageView ivDelete;
        public LinearLayout lay;
    }
}
